package artid.com.arttogoa.adapter;

import java.util.List;
import artid.com.arttogoa.R;
import artid.com.arttogoa.util.CircleImageView;
import artid.com.arttogoa.util.NetUtils;
import artid.com.arttogoa.util.VeDate;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMConversation;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.chat.EMMessage.Type;
import com.hyphenate.chat.EMTextMessageBody;
import com.nostra13.universalimageloader.core.ImageLoader;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * 
 *    
 * 项目名称：ArtworkArtA   
 * 类名称   ：RealTimeMessageAdapter   
 * 类描述   ：实时消息适配器  
 * 创建人   ：耿卫斌   
 * 创建时间：2016-5-25 下午2:41:21   
 *
 */
public class RealTimeMessageAdapter extends BaseAdapter{
	private Context mContext;
	private List<EMConversation> list;

	public RealTimeMessageAdapter(Context mContext,List<EMConversation> list) {
		this.mContext = mContext;
		this.list = list;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		int countNum=0;
		if(list.size()>=3){
			countNum=3;
		}else{
			countNum=list.size();
		}
		return countNum;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder = null;
		View v = convertView;
		if (v == null) {
			holder = new ViewHolder();
			v = LayoutInflater.from(mContext).inflate(R.layout.item_realtimemessage, null);
			holder.informationHead=(CircleImageView) v.findViewById(R.id.item_realtimemessage_informationHead);
			holder.unreadMessage_icon=(ImageView) v.findViewById(R.id.item_realtimemessage_unreadMessage);
			holder.SystemMessageHead=(ImageView) v.findViewById(R.id.item_realtimemessage_SystemMessageHead);
			holder.ReplyHead=(ImageView) v.findViewById(R.id.item_realtimemessage_ReplyHead);
			holder.ReplImg=(ImageView) v.findViewById(R.id.item_realtimemessage_ReplImg);
			holder.informationTitle=(TextView) v.findViewById(R.id.item_realtimemessage_informationTitle);
			holder.informationTime=(TextView) v.findViewById(R.id.item_realtimemessage_informationTime);
			holder.informationContent=(TextView) v.findViewById(R.id.item_realtimemessage_informationContent);
			holder.SystemMessageTitle=(TextView) v.findViewById(R.id.item_realtimemessage_SystemMessageTitle);
			holder.SystemMessageTime=(TextView) v.findViewById(R.id.item_realtimemessage_SystemMessageTime);
			holder.SystemMessageContent=(TextView) v.findViewById(R.id.item_realtimemessage_SystemMessageContent);
			holder.ReplyName=(TextView) v.findViewById(R.id.item_realtimemessage_ReplyName);
			holder.ReplyTime=(TextView) v.findViewById(R.id.item_realtimemessage_ReplyTime);
			holder.ReplyComment=(TextView) v.findViewById(R.id.item_realtimemessage_ReplyComment);
			holder.ReplyTitle=(TextView) v.findViewById(R.id.item_realtimemessage_ReplyTitle);
			holder.informationLL=(LinearLayout) v.findViewById(R.id.item_realtimemessage_informationLL);
			holder.SystemMessageLL=(LinearLayout) v.findViewById(R.id.item_realtimemessage_SystemMessageLL);
			holder.ReplyLL=(LinearLayout) v.findViewById(R.id.item_realtimemessage_ReplyLL);
			v.setTag(holder);
		} else {
			holder = (ViewHolder) v.getTag();
		}
		holder.informationLL.setVisibility(View.VISIBLE);
		holder.SystemMessageLL.setVisibility(View.GONE);		
		holder.ReplyLL	.setVisibility(View.GONE);	

		String friendAvatar = "";//好友头像
		String fromName="";//好友ID（也是名字）
		holder.informationTime.setText(VeDate.getLnow(list.get(position).getLastMessage().getMsgTime()));
		Type type = list.get(position).getLastMessage().getType();
		if(type==EMMessage.Type.TXT){
			EMTextMessageBody txtBody = (EMTextMessageBody) list.get(position).getLastMessage().getBody();
			holder.informationContent.setText(txtBody.getMessage());
		}else{
			holder.informationContent.setText("/图片/");
		}
		friendAvatar=list.get(position).getLastMessage().getStringAttribute("artistURL","http://172.16.0.3/upload/artPlatform/Addons/Avatar/default_128_128.png");
		holder.informationHead.setTag(friendAvatar);
		ImageLoader.getInstance().displayImage(friendAvatar
				+ "_"+ NetUtils.dip2px(mContext, 100)+"x"+NetUtils.dip2px(mContext, 100)+".png"
				, holder.informationHead);
		fromName=list.get(position).getLastMessage().getStringAttribute("fromName",list.get(position).conversationId());
		holder.informationTitle.setText(fromName);
		EMConversation conversation = EMClient.getInstance().chatManager().getConversation(list.get(position).conversationId());
		if(conversation.getUnreadMsgCount()!=0){
			holder.unreadMessage_icon.setVisibility(View.VISIBLE);
		}else{
			holder.unreadMessage_icon.setVisibility(View.GONE);
		}
		return v;
	}
	class ViewHolder {
		CircleImageView informationHead;
		ImageView unreadMessage_icon,SystemMessageHead,ReplyHead,ReplImg;
		TextView informationTitle,informationTime,informationContent;
		TextView SystemMessageTitle,SystemMessageTime,SystemMessageContent;
		TextView ReplyName,ReplyTime,ReplyComment,ReplyTitle;
		LinearLayout informationLL,SystemMessageLL,ReplyLL;
	}

}
